package com.jindy.module.machine.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.jindy.module.machine.domain.Machine;
import com.jindy.module.machine.domain.MachineRecord;
import com.jindy.module.machine.domain.machinevo.MachineVo;
import com.jindy.module.machine.dto.MachineDto;
import com.jindy.module.machine.dto.MachineRecordDto;
import com.jindy.module.machine.mapper.MachineRecordMapper;
import com.jindy.module.machine.service.IMachineRecordService;
import com.jindy.module.order.domain.Order;
import com.jindy.module.order.mapper.OrderMapper;
import com.jindy.module.project.domain.Project;
import com.jindy.module.project.mapper.ProjectMapper;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 机器使用记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-10-21
 */
@Service
public class MachineRecordServiceImpl implements IMachineRecordService
{
    @Autowired
    private MachineRecordMapper machineRecordMapper;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private ProjectMapper projectMapper;

    /**
     * 查询机器使用记录
     *
     * @param id 机器使用记录主键
     * @return 机器使用记录
     */
    @Override
    public MachineRecord selectMachineRecordById(Long id)
    {
        return machineRecordMapper.selectMachineRecordById(id);
    }

    /**
     * 查询机器使用记录列表
     *
     * @param machineRecord 机器使用记录
     * @return 机器使用记录
     */
    @Override
    public List<MachineRecord> selectMachineRecordList(MachineRecord machineRecord)
    {
        return machineRecordMapper.selectMachineRecordList(machineRecord);
    }

    @Override
    public List<MachineRecord> machineListByOrderId(MachineRecord machineRecord) {
        return machineRecordMapper.machineListByOrderId(machineRecord);
    }

    @Override
    public List<MachineRecordDto> machineListByMachineId(MachineDto machineDto) {
        return machineRecordMapper.machineListByMachineId(machineDto);
    }

    /**
     * 查询机器在哪些项目中使用过
     *
     * @param machineRecord 机器使用记录
     * @return 结果
     */
    @Override
    public List<MachineVo> selectMachineUseRecordList(MachineRecord machineRecord){
        List<MachineRecord> machineRecordList = machineRecordMapper.selectMachineRecordList(machineRecord);//只需传入机器编号即可
        if(machineRecordList.size() == 1){//只有采购入库时的一条记录
            throw  new RuntimeException("此机器无使用记录");
        }
        List<MachineVo> machineVoList = new ArrayList<>();
        machineRecordList.forEach(record->{

            //过滤采购机器时的入库记录
            if(record.getOrderNo() != null){
                MachineVo machineVo =new MachineVo();

                //通过订单id找到相应的订单
                Order order = orderMapper.selectOrderById(record.getOrderId());

                //通过订单中的项目名称，查找项目id
                Project project = new Project();
                project.setProjectName(order.getProjectName());
                List<Project> projectList = projectMapper.selectProjectList(project);
                Project project1 = projectList.get(0);

                machineVo.setOrderId(order.getId());
                machineVo.setOrderNo(order.getOrderNo());
                machineVo.setProjectId(Long.valueOf(project1.getId()));
                machineVo.setProjectName(order.getProjectName());
                machineVo.setMachineNo(record.getMachineNo());

                //通过订单编号和机器编号查询对应的出入库记录
                MachineRecord machineRecord1 = new MachineRecord();
                machineRecord1.setOrderId(record.getOrderId());
                machineRecord1.setOrderNo(record.getOrderNo());
                machineRecord1.setMachineNo(record.getMachineNo());
                List<MachineRecord> machineRecordList1 = machineRecordMapper.selectMachineRecordList(machineRecord1);

                //如果机器正在使用，尚未入库，则只有出库记录
                if(machineRecordList1.size() == 1){
//                    machineVo.setWarehousingTime(record2.getWarehousingTime();
                    MachineRecord record1 = machineRecordList1.get(0);//出库记录
                    machineVo.setOutboundTime(record1.getOutboundTime());
                    machineVoList.add(machineVo);
                }else{
                    MachineRecord record2 = machineRecordList1.get(1);//出库记录
                    MachineRecord record3 = machineRecordList1.get(0);//入库记录
                    machineVo.setOutboundTime(record2.getOutboundTime());
                    machineVo.setWarehousingTime(record3.getWarehousingTime());
                    machineVoList.add(machineVo);
                    }
            }
        });
        return machineVoList;
    }

    /**
     * 新增机器使用记录
     *
     * @param machineRecord 机器使用记录
     * @return 结果
     */
    @Override
    public int insertMachineRecord(MachineRecord machineRecord)
    {
        return machineRecordMapper.insertMachineRecord(machineRecord);
    }

    /**
     * 修改机器使用记录
     *
     * @param machineRecord 机器使用记录
     * @return 结果
     */
    @Override
    public int updateMachineRecord(MachineRecord machineRecord)
    {
        return machineRecordMapper.updateMachineRecord(machineRecord);
    }

    /**
     * 批量删除机器使用记录
     *
     * @param ids 需要删除的机器使用记录主键
     * @return 结果
     */
    @Override
    public int deleteMachineRecordByIds(Long[] ids)
    {
        return machineRecordMapper.deleteMachineRecordByIds(ids);
    }

    /**
     * 删除机器使用记录信息
     *
     * @param id 机器使用记录主键
     * @return 结果
     */
    @Override
    public int deleteMachineRecordById(Long id)
    {
        return machineRecordMapper.deleteMachineRecordById(id);
    }
}
